package net.caixiaomi.info.ui.withdraw;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.UserInfoModel;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.info.widgets.BankCardTextWatcher;
import net.caixiaomi.info.widgets.DefLoading;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;


/**
 * 添加银行卡
 */

public class AddCardActivity extends BaseActivity {
    //显示用户实名认证的姓名信息
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.card)
    EditText mCard;

    @OnClick(R.id.btn_done)
    void toAddDone() {
        AppHelper.hideKeyboard(getCurrentFocus());
        String card = mCard.getText().toString().trim().replaceAll(" ", "");
        if (card.length() < Constants.BANK_CARD_MIN_SIZE) {//银行卡最小位数
            ToastUtil.s(R.string.C_WITHDRAW_CARD_INCORRECT);
            return;
        }
        AppHelper.sendStatistics("yhktianjia", new String[]{getString(R.string.C_TERMINAL)}, new String[]{Build.BOARD});
        mLoading.show();
        JSONObject params = new JSONObject();
        params.put("bankCardNo", card);
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel> login = requestService.addBankCard(params);
        login.enqueue(new ResponseCallback<BaseCallModel>(this) {
            @Override
            public void onSuccess(BaseCallModel data) {
                mLoading.dismiss();
                EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_ADD_BANK_CARD));
                AddCardActivity.super.onBackPressed();
                ToastUtil.s(data.msg);
            }

            @Override
            public void onFail(ResponseError error) {
                mLoading.dismiss();
                if (error.getCode() >= 300000 && error.getCode() <= 310000) {
                    ToastUtil.s(error.getMessage());
                }
            }

            @Override
            public void onError() {
                mLoading.dismiss();
            }
        });

    }

    @Override
    protected int layout() {
        return R.layout.act_add_card;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoading = new DefLoading(this);
        UserInfoModel userInfo = CommonApp.getUserInfo();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getString(R.string.C_NAME)).append(": ").append(userInfo.getRealName());
        SpannableString spannableString = new SpannableString(stringBuilder);
        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text)), stringBuilder.length() - userInfo.getRealName().length(), stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        mName.setText(spannableString);
        mCard.setFilters(new InputFilter[]{new InputFilter.LengthFilter(23)});
        BankCardTextWatcher watcher = new BankCardTextWatcher(mCard);
        mCard.addTextChangedListener(watcher);
        mCard.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                AppHelper.sendStatistics("shuruyhk");
                return false;
            }
        });
    }

}
